//
//  ExpressInforViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/7.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ExpressInforViewModel.h"
#import "ExpressInforTableViewCell.h"
#import "ExpressInforModel.h"

@interface ExpressInforViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *selfTableView;
}
@property (nonatomic, strong) ExpressInforModel *model;

@end

@implementation ExpressInforViewModel

 - (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.estimatedRowHeight = 20;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.tableHeaderView = [self createHeaderView];
        [superView addSubview:tableView];
        selfTableView = tableView;
    }
    return self;
}

 - (UIView *)createHeaderView
{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return headerView;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.model) {
        return self.model.data.count;
    }
    return 0;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExpressInforTableViewCell *cell = [ExpressInforTableViewCell cellWithTableView:tableView];
    if (self.model) {
        [cell refreshExpressInforTableViewCell:self.model index:indexPath];
    }
    return cell;
}

- (void)setOrderID:(NSString *)orderID
{
    _orderID = orderID;
    [self requestOrderTracking];
}

#pragma mark - 请求接口
- (void)requestOrderTracking
{
    NSDictionary *param = @{@"orderId" : koString(self.orderID)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kMyOrderOrderTracking] lodingView:nil lodingTextString:@"" isLoding:YES parameters:param success:^(id  _Nullable response) {
        self.model = [ExpressInforModel yy_modelWithJSON:response];        
        [self->selfTableView reloadData];
    } failure:^(NSError * _Nullable error) {
    }];

}

@end
